package com.hxq.ext.delayqueue.demo1;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.DelayQueue;

/**
 * DelayQueue是JDK提供的api，是一个延迟队列
 */
@Slf4j
public class DelayQueueDemo {

    public static void main(String[] args) {
        DelayQueue<MyDelayTask> delayQueue = new DelayQueue<>();

        CompletableFuture.runAsync(()->{
            while (true){
                try {
                    MyDelayTask task = delayQueue.take();
                    log.info("获取到延时任务：{}",task.getTaskContent());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        log.info("提交延时任务");

        delayQueue.offer(new MyDelayTask("5",5L));
        delayQueue.offer(new MyDelayTask("3",3L));
        delayQueue.offer(new MyDelayTask("8",8L));

        while (true){

        }

    }
}
